package Str;

import java.util.HashMap;

/**
 * @auther wangchenghao
 * @date 2021/6/7
 *
 * 字符串中的第一个唯一字符
 **/
public class test3 {
    public static int firstUniqChar(String s) {
        HashMap<Character,Integer> hashMap=new HashMap<>();
        char[] chars=s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            hashMap.put(chars[i],hashMap.getOrDefault(chars[i],0)+1);
        }
        for (int i = 0; i < chars.length; i++) {
            if (hashMap.get(chars[i])==1){
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        System.out.println("firstUniqChar(\"hahsda\") = " + firstUniqChar("hahsda"));
    }
}
